<?php

namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Acme\DemoBundle\Entity\Lydonghiviec;

class LydonghiviecController extends Controller
{
	private function getlist ($index,$thongbao) {
		$listLydonghiviec =	$this->getDoctrine()
		->getRepository('AcmeDemoBundle:Lydonghiviec')
		->findByCoditions(10*($index-1),10);
			
		$countLydonghiviec = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:Lydonghiviec')->countAll();
			
		$count = \Count($countLydonghiviec)/10;
			
		$mod = \Count($countLydonghiviec)%10;
			
		if($mod>0){
			$count++;
		}

		return $this->render(
				'AcmeDemoBundle:Demo:Lydonghiviec_list.html.twig',
				array('listLydonghiviec' => $listLydonghiviec,
						'index' => $index,
						'count' => $count,
						'thongbao' => $thongbao
				)
		);
	}
	public function inputAction()
	{
		$request = $this->get('request');
		if ($request->isMethod('POST')) {
			
			$ten = $request->request->get('ten');
			$ma = $request->request->get('ma');
			
			$check = $this->getDoctrine()
			->getRepository('AcmeDemoBundle:Lydonghiviec')
			->checkMaInsert($ma);
			
			if($check){
				return $this->render(
						'AcmeDemoBundle:Demo:Lydonghiviec_input.html.twig',
						array('exist' => "ok",
								'ma' => $ma,
								'ten' => $ten
			
						)
				);
			}
			
			$Lydonghiviec = new Lydonghiviec();
			$Lydonghiviec->setTen($ten);
			$Lydonghiviec->setMa($ma);
			$Lydonghiviec->setCreated(new \DateTime('now'));
			$this->getDoctrine()
			->getRepository('AcmeDemoBundle:Lydonghiviec')
			->saveLydonghiviec($Lydonghiviec);


			return $this->getlist(1,"ok");

		}
		return $this->render(
				'AcmeDemoBundle:Demo:Lydonghiviec_input.html.twig',
						array('exist' => null,
								'ma' => "",
								'ten' => ""
			
						)
		);
	}
	public function listAction($index)
	{
		return $this->getlist($index,null);
		
	}
	public function detailAction($id)
	{
		$Lydonghiviec = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:Lydonghiviec')
		->find($id);
		 
		return $this->render(
				'AcmeDemoBundle:Demo:Lydonghiviec_edit.html.twig',
				array('Lydonghiviec' => $Lydonghiviec,'exist' => null)
		);
	}
	public function editAction($id)
	{
		$Lydonghiviec = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:Lydonghiviec')
		->find($id);
		$request = $this->get('request');
		$ma = $request->request->get('ma');
		$ten = $request->request->get('ten');
		
		if($Lydonghiviec->getMa() != $ma) {
			$Check = $this->getDoctrine()
			->getRepository('AcmeDemoBundle:Lydonghiviec')
			->checkMaUpdate($ma,$Lydonghiviec->getMa());
			if($Check){
				return $this->render(
						'AcmeDemoBundle:Demo:Lydonghiviec_edit.html.twig',
						array('Lydonghiviec' => $Lydonghiviec,'exist' => 'ok')
				);
			}
		}

		$Lydonghiviec->setTen($ten);
		$Lydonghiviec->setMa($ma);
		
		$Lydonghiviec->setUpdated(new \DateTime('now'));
		$this->getDoctrine()
		->getRepository('AcmeDemoBundle:Lydonghiviec')
		->saveLydonghiviec($Lydonghiviec);

		return $this->getlist(1,"ok");


	}
	public function delAction($id)
	{
		$Lydonghiviec = $this->getDoctrine()
		->getRepository('AcmeDemoBundle:Lydonghiviec')
		->find($id);
		
		$Lydonghiviec->setDeleted(new \DateTime('now'));
		
		$this->getDoctrine()
		->getRepository('AcmeDemoBundle:Lydonghiviec')
		->saveLydonghiviec($Lydonghiviec);

		return $this->getlist(1,"ok");
		
	}

}
